//
//  Collection.m
//  UI_12_TableViewController
//
//  Created by dllo on 15/9/17.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "Collection.h"
#import "MyCell.h"
//#import "UIImageView+WebCache.h"
@interface Collection ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation Collection

- (instancetype)init
{
    self = [super init];
    if (self) {
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    return self;
}
- (void)setImageArr:(NSArray *)imageArr
{
    _imageArr = imageArr;
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumLineSpacing = 0;
    self.collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flow];
    
    [self addSubview:self.collection];
    
    [self.collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"shime"];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.pagingEnabled = YES;
    self.collection.contentOffset = CGPointMake(self.bounds.size.width, 0);
    self.collection.bounces = NO;
    [self addSeg];
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer
{
    NSInteger i = self.collection.contentOffset.x/self.bounds.size.width;
    self.collection.contentOffset = CGPointMake(self.bounds.size.width*i, 0);
    if (self.collection.contentOffset.x < self.bounds.size.width*self.imageArr.count) {
        [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x + self.bounds.size.width, 0) animated:YES];
    }
    
        self.collection.contentOffset = CGPointMake(0, 0);
        [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x + self.bounds.size.width, 0) animated:YES];
        CATransition *transtion = [CATransition animation];
        transtion.type = @"rippleEffect";/* 效果:pageCurl pageUnCurl  kCATransitionFade  cameraIrisHollowOpen */
        transtion.autoreverses = YES;
        transtion.duration = 3;
        transtion.repeatCount = 5;
        /* layer层添加动画 */
    NSLog(@" tu:%ld",self.imageArr.count);
        [self.collection.layer addAnimation:transtion forKey:@"cc"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count + 2;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shime" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        [cell.imageView setImage:[UIImage imageNamed:@"5"]];
//        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr.lastObject] placeholderImage:[UIImage imageNamed:@"占位图.jpg"]];
    }
    else if (indexPath.item == self.imageArr.count + 1){
        [cell.imageView setImage:[UIImage imageNamed:@"5"]];

//        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr.firstObject] placeholderImage:[UIImage imageNamed:@"占位图.jpg"]];
    }
    else{
        [cell.imageView setImage:[UIImage imageNamed:@"5"]];

//        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.imageArr objectAtIndex:indexPath.item-1]] placeholderImage:[UIImage imageNamed:@"占位图.jpg"]];
    }

    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.seg.currentPage = scrollView.contentOffset.x/self.bounds.size.width - 1;
}
- (void)addSeg
{
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.imageArr.count; i ++) {
        NSString *str = [NSString stringWithFormat:@"%ld",(long)i];
        [arr addObject:str];
    }
    self.seg = [[UIPageControl alloc] init];
    self.seg.frame = CGRectMake(self.bounds.size.width-self.imageArr.count*20, self.bounds.size.height - 20, 20*self.imageArr.count, 20);
    [self addSubview:self.seg];
    self.seg.numberOfPages = self.imageArr.count;
    self.seg.pageIndicatorTintColor = [UIColor blackColor];
    self.seg.currentPageIndicatorTintColor = [UIColor redColor];
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)segAction:(UIPageControl *)seg
{
    [self.collection setContentOffset:CGPointMake(self.bounds.size.width * (seg.currentPage+1), 0) animated:YES];
}
@end
